drop processingdate		// variable for version control only
drop restrictiondetail	// very detailed info, leave out for now
drop clientid			// single-valued, pretty sure it means us
drop therapeuticclass	// already in another dataset

* destring IDs. For some, attach names as values. Otherwise, drop names
destring formularyid, replace
destring medid, replace

* format period as a monthly variable
gen m = ym(real(substr(period,1,4)), real(substr(period,-2,2)))
format m %tm
drop period

* reshape wide (there is a separate observation for each restriction, we want to 
* keep them together)
bysort formularyid medid m pull (restrictioncode) : gen seq = _n
reshape wide restrictioncode, ///
	i(formularyid medid m pull) j(seq)

* generate unique restriction variable
gen restrictioncodes = restrictioncode1
replace restrictioncodes = restrictioncodes + "," + ///
						restrictioncode2 if restrictioncode2 != ""
replace restrictioncodes = restrictioncodes + "," + ///
						restrictioncode3 if restrictioncode3 != ""

drop restrictioncode1 restrictioncode2 restrictioncode3

* now impute missing months within the same year
gen year = yofd(dofm(m))
gen month = month(dofm(m))

egen double xID = group(medid formularyid year)
xtset xID month

tsfill, full

* fill in
foreach var in medid formularyid year {
	bysort xID (`var') : replace `var' = `var'[1] if `var' == .
	}

replace m = mofd(dofy(year)) - 1 + month if m == .

drop xID month
compress

* for each medid, drop leading and trailing observations (i.e., before launch 
* and after it has been withdrawn)
bysort medid : egen last = max(m * (restrictioncodes != ""))
bysort medid : egen first = min(m * (restrictioncodes != "") + 9999999 * (restrictioncodes == ""))

drop if m > last | m < first
drop first last

compress

* fill in
gen flag_impute = restrictioncodes == ""

bysort formularyid medid year (m) : replace restrictioncodes = ///
	restrictioncodes[_n-1] if restrictioncodes == "" & restrictioncodes[_n-1] != ""

gen negm = -m
bysort formularyid medid year (negm) : replace restrictioncodes = ///
	restrictioncodes[_n-1] if restrictioncodes == "" & restrictioncodes[_n-1] != ""

drop negm
compress
